package cn.itcast.e_processVariablesHistory;

import java.util.List;

import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.history.HistoricActivityInstance;
import org.activiti.engine.history.HistoricActivityInstanceQuery;
import org.activiti.engine.history.HistoricProcessInstance;
import org.activiti.engine.history.HistoricTaskInstance;
import org.activiti.engine.history.HistoricVariableInstance;
import org.junit.Test;

public class HistoryQueryTest {
	
	ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
	
	/**
	 * 查询历史流程实例
	 */
	@Test
	public void findHistoryProcessInstance(){
		String pri_id="1701";
		HistoricProcessInstance singleResult = processEngine.getHistoryService()
						.createHistoricProcessInstanceQuery()
						.processInstanceId(pri_id)
						.orderByProcessInstanceStartTime().asc()
						.singleResult();
		System.out.println("流程实例: "+singleResult);
	}
	/**
	 * 查询历史活动
	 * act_hi_actinst
	 */
	@Test
	public void findHistoryActiviti(){
		String pri_id="1701";
		List<HistoricActivityInstance> list = processEngine.getHistoryService()
						.createHistoricActivityInstanceQuery()
						.processInstanceId(pri_id)
						.orderByHistoricActivityInstanceStartTime()
						.asc()
						.list();
		
		System.out.println("活动实例: "+list);
	}
	/**
	 * 查询历史任务
	 */
	@Test
	public void findHistoryTask(){
		String pri_id="1701";
		List<HistoricTaskInstance> list = processEngine.getHistoryService()//与历史数据相关的service
						.createHistoricTaskInstanceQuery()//创建历史任务实例查询
						.processInstanceId(pri_id)
						.list();
		
		if(list.size()>0){
			for(HistoricTaskInstance hi:list){
				System.out.println("任务id: "+hi.getId());
				System.out.println("任务名称: "+hi.getName());
				System.out.println("流程实例id: "+hi.getProcessInstanceId());
				System.out.println("开始时间: "+hi.getStartTime());
				System.out.println("持续时间: "+hi.getDurationInMillis());
				
			}
		}
	}
	/**
	 *查询流程变量历史表
	 */
	@Test
	public void findProcessVarialbeHis(){
		String pri_id="1701";
		List<HistoricVariableInstance> list = processEngine.getHistoryService()
						.createHistoricVariableInstanceQuery()//创建一个流程变量的历史查询
						.processInstanceId(pri_id)
						.list();
		if(list.size()>0){
			for(HistoricVariableInstance hv:list){
				System.out.println("流程实例："+hv.getProcessInstanceId());
				System.out.println("变量id："+hv.getId());
				System.out.println("变量名称："+hv.getVariableName());
				System.out.println("任务id："+hv.getTaskId());
			}
		}
		
	}
	
}
